package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingcartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: ShoppingcartServiceImpl
 * Package: IntelliJ IDEA
 *
 * @author 张文豪
 * @description
 * @date 2023/8/18 14:51
 */
@Service
public class ShoppingcartServiceImpl implements ShoppingcartService {
    @Autowired
    private ShoppingCartMapper shoppingcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //先查是否有相同的购物车数据
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(shoppingCartDTO.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId()).eq(shoppingCartDTO.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor())
                .eq(shoppingCartDTO.getDishId() != null,ShoppingCart::getDishId,shoppingCartDTO.getDishId());
        List<ShoppingCart> shoppingCarts = shoppingcartMapper.selectList(lambdaQueryWrapper);
        if(shoppingCarts != null && shoppingCarts.size()  > 0){
            //数量加一
            ShoppingCart shoppingCart = shoppingCarts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //价格加一下
//            if(shoppingCart.getSetmealId() != null){
//                BigDecimal price1 = setmealMapper.selectById(shoppingCart.getSetmealId()).getPrice();
//                BigDecimal price2 = shoppingCart.getAmount();
//                shoppingCart.setAmount(price2.add(price1));
//            }else {
//                BigDecimal price1 = dishMapper.selectById(shoppingCart.getDishId()).getPrice();
//                BigDecimal price2 = shoppingCart.getAmount();
//                shoppingCart.setAmount(price2.add(price1));
//            }
            //更新数据
            shoppingcartMapper.updateById(shoppingCart);
            return;
        }
        //查不到给里边添加一条数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
        if(shoppingCart.getDishId() != null){
            Dish dish = dishMapper.selectById(shoppingCart.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        }else{
            Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }

        shoppingcartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> listShopCart() {
        List<ShoppingCart> shoppingCarts = shoppingcartMapper.selectList(null);
        return shoppingCarts;
    }

    @Override
    public void clean() {
        shoppingcartMapper.delete(null);
    }

    @Override
    public void deleteSingle(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(shoppingCartDTO.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(ShoppingCart::getUserId, BaseContext.getCurrentId()).eq(shoppingCartDTO.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor())
                .eq(shoppingCartDTO.getDishId() != null,ShoppingCart::getDishId,shoppingCartDTO.getDishId());
        List<ShoppingCart> shoppingCarts = shoppingcartMapper.selectList(lambdaQueryWrapper);

            ShoppingCart shoppingCart = shoppingCarts.get(0);
            if(shoppingCart.getNumber() > 1){
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                //todo amount数据的相减问题
//            if(shoppingCart.getSetmealId() != null){
//                BigDecimal price = setmealMapper.selectById(shoppingCart.getSetmealId()).getPrice();
//                BigDecimal sumPrice = shoppingCart.getAmount();
//                shoppingCart.setAmount(sumPrice.subtract(price));
//            }else {
//                BigDecimal price = dishMapper.selectById(shoppingCart.getDishId()).getPrice();
//                BigDecimal sumPrice = shoppingCart.getAmount();
//                shoppingCart.setAmount(sumPrice.subtract(price));
//            }
            shoppingcartMapper.updateById(shoppingCart);
            }else {
                shoppingcartMapper.deleteById(shoppingCart.getId());
            }
        }
}
